<template>
	<view class="goods_list">
		<goods-list @goodsItemClick="goGoodsDetail" :goods="goods"></goods-list>
		<view class="isOver" v-if="flag">已经到底啦!</view>
	</view>
</template>

<script>
	import goodsList from '../../components/goods-list/goods-list.vue'
	export default {
		data() {
			return {
				pageindex: 1,
				goods: [],
				flag: false
			}
		},
		components: {'goods_list': goodsList},
		methods: {
			async getGoodsList(callBack) {
				const res = await this.$myRequest({
					url: '/api/getgoods?pageindex=' + this.pageindex
				})
				console.log(res)
				this.goods = [...this.goods,...res.data.message]
				// 如果有callBack 就调用 否则不调用
				callBack && callBack()
			},
			// 导航到商品详情页面
			goGoodsDetail(id) {
				uni.navigateTo({
					url: '/pages/goods-detail/goods-detail?id=' + id 
				})
			}
		},
		onLoad() {
			this.getGoodsList()
		},
		// 触底后进行的动作
		onReachBottom(){
			// 获取下一页的数据
			// 如果thisgoods的长度小于10，就意味着没有下一页
			if(this.goods.length < this.pageindex * 10) return this.flag = true
			this.pageindex++
			this.getGoodsList()
		},
		// 监听下拉刷新
		onPullDownRefresh(){
			this.pageindex = 1
			this.goods = []
			this.flag = false
			setTimeout(() => {
				this.getGoodsList(() => {
					uni.stopPullDownRefresh()
				})
			},1000)
		}
	}
</script>

<style lang="scss">
.goods_list{
	background-color: #eee;
	.isOver{
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		font-size: 28rpx;
	}
}
</style>
